<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
use Common\Model\UserModel;
use Admin\Form\UserForm;
use Common\Model\PublicNetworkModel;
use Common\Api\Reward;
use Common\Model\LeavelModel;
use Common\Model\UserMoneyModel;
use Common\Model\CurrencyModel;
use Common\Model\UserBankModel;
use Common\Model\FinanceLogModel;
use Common\Model\RegLogModel;
class UserController extends AdminBaseController{
	public function showlist()
	{
	    if ($_GET['username']) {
// 	       $user=UserForm::newSelf()->search($input);
           $username=I('username'); 
           $user=$this->findUserByUserName($username);
	       $where['id']=$user['id'];
	    }
	    $list=UserModel::newSelf()->pagingList($where);
	    $this->assign('list',$list);
		$this->display();
	}
	public function update()
	{
        $username=I('username'); 
        $user=$this->findUserByUserName($username);
	    $model=UserModel::newSelf();
	    $leavel=LeavelModel::newSelf()->getInfo();
	    $this->assign('level',$leavel);
	    $this->assign('info',$user);
	    $this->display();
	}
	public function updateFormSub()
	{
	   $input=I('post.');
        $this->notEmptyValidate($input['id'], '请选择用户');
        
        $model=UserModel::newSelf();
        
        $data['phone']=I('phone');
        $data['email']=I('email');
        $data['realname']=I('realname');
        $data['idcard']=I('idcard');
        $data['address']=I('address');
        $data['wechat']=I('wechat');
        $data['qq']=I('qq');
        $data['tel']=I('tel');
        $data['lock']=I('lock');
        $data['leavel']=I('level','','intval');
        
        if ($input['password']) {
           $data['password']=$model->passwordEncrypt($input['password']);
        }
        if ($input['twopassword']) {
           $data['twopassword']=$model->twoPasswordEncrypt($input['twopassword']);
        }
        
        $res=$model->where(['id'=>$input['id']])->save($data);
        $this->resReturn($res, '修改成功', '修改失败');
	}
	public function goToHome()
	{
	    $input=I('username');
	    $info=$this->findUserByUserName($input, '用户不存在');
	    $_SESSION['USER_KEY_ID']=$info['id'];
	    $this->redirect('Home/User/index');
	}
	/**
	 *注册会员页面 
	 */
	public function addUser(){
        $username=I('username'); 
        $user=$this->findUserByUserName($username);
	    $model=UserModel::newSelf();
	    $this->assign('info',$user);
	    

	    $leavel_type=LeavelModel::newSelf()->getInfo();
	    $this->assign('leavel_type',$leavel_type);
		$this->display();
	}
	public function addFormSub(){
		$data['username']=rtrim(I('username'));
		$data['phone']=I('phone');
		$data['tuiname']=I('tuiname');
		$data['jiename']=I('jiename');
		$data['quyu']=(int)I('quyu');
		$data['leavel']=I('leavel');
		$this->notEmptyValidate($data['username'],'用户名不能为空');
		$this->notEmptyValidate($data['phone'],'电话号码不能为空');
		$this->notEmptyValidate($data['tuiname'],'推荐人不能为空');
		$this->notEmptyValidate($data['jiename'],'安置人不能为空');
		$this->notEmptyValidate($data['quyu'],'请选择安置位置');
		$this->notEmptyValidate($data['leavel'],'请选择会员等级');
		
		$leavel_type=LeavelModel::newSelf()->getInfo();
		$this->notEmptyValidate($leavel_type[$data['leavel']],'请正确选择会员等级');

		$num=$leavel_type[$data['leavel']]['money'];
		
		$need_currency=1;//账户余额
	
		$model=UserModel::newSelf();
		
		$user=$model->findUserByUsername($data['username']);
		$this->emptyValidate($user,'用户名已存在');
		
		$countPhone=$model->countByPhone($data['phone']);
		$this->ltValidate($countPhone,5,'该手机号码已超过账户绑定限制');
		
		$tui=$model->findUserByUsername($data['tuiname']);
		$this->notEmptyValidate($tui,'推荐人不存在');
		$this->neqValidate($tui['lock'],1,'推荐人账号被冻结');
		unset($data['tuiname']);
		$data['tui_id']=$tui['id'];
		
		$jie=$model->findUserByUsername($data['jiename']);
		$this->notEmptyValidate($jie,'接点人不存在');
		$this->neqValidate($jie['lock'],1,'接点人账号被冻结');
		unset($data['jiename']);
		$data['jie_id']=$jie['id'];
		$data['activation']=1;
		if ($data['quyu']!='1') {
			$left_user=$model->findUserByJieIdAndQuyu($jie['id'], 1);
			$this->notEmptyValidate($left_user,'该用户左区还没有用户');
		}
		
		$check=$model->findUserByJieIdAndQuyu($jie['id'],$data['quyu']);
		$this->emptyValidate($check,'此区域已经有人');
		
// 		$currency=CurrencyModel::newSelf()->getInfo();
// 		$user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
		
// 		$num=$leavel_type[$data['leavel']]['money'];
		
// 		$this->egtValidate($user_money[$need_currency]['num'],$num,'您的'.$currency[$need_currency]['name'].'不足');
		
		$data['password']=$model->passwordEncrypt('123456');
		$data['twopassword']=$model->twoPasswordEncrypt('123456');
		$data['reg_time']=time();
		$user_id=$model->add($data);
		
		$bank_data['user_id']=$user_id;
		$bank_data['add_time']=time();
		UserBankModel::newSelf()->add($bank_data);
		
		$money_model=UserMoneyModel::newSelf();
		$money_model->initializationMoneyInfo($user_id,$data['leavel']);
		
// 		$reward= Reward::newSelf($user_id,$num);
		
// 		$money_model->decUserMoney($this->home_user_info['id'], $need_currency, $num);
		
// 		$bonus_id=$reward->updateBonusLog(0,['bonus5'=>$num]);
// 		FinanceLogModel::newSelf()->addLog(100, $num, 0, $need_currency,$user_id,$bonus_id);
		 
		$data['user_id']=0;
		$data['reg_type']=$need_currency;
		$data['num']=$num;
		$data['reg_id']=$user_id;
		$data['leavel']=$data['leavel'];
		$data['add_time']=time();
		$data['type']=1;
		RegLogModel::newSelf()->add($data);
		
		
		//         $reward->anzhijiang();
// 		$reward->initialTree();
		//         $reward->jiandianjiang();
		//         $reward->duipeng();
// 		$reward->guanlijiang();
// 		$reward->tuijianjiang();
		//         $user_money=$money_model->getUserMoneyInfo($user_id);
		//         $this->tradeBuyBeginByMoney($user_id, $user_money['11']['num'],$currency['11'],$currency['1']);
		
		
		$this->resReturn($user_id, '注册成功', '注册失败');
	}
	/**
	 *空点激活 
	 */
	public function activation(){
		$username=rtrim(I('username'));
	    $user=$this->findUserByUserName($username);
	    $this->notemptyValidate($user['id'],'用户不存在');
	    $this->notemptyValidate($user['leavel'],'用户等级错误');
	    $level_info=LeavelModel::newSelf()->findOneById($user['leavel']);
	    $reward= Reward::newSelf($user['id'],$level_info['money']);
	    $reward->initialTree();
	    $reward->guanlijiang();
	    $reward->tuijianjiang();
	    $res=UserModel::newSelf()->where(['id'=>$user['id']])->setField('activation',0);
	    	    
        $this->resReturn($res, '激活成功', '激活失败');
	}
}
